package cn.com.zsc.system;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.com.zsc.system.vo.GroupVO;
import cn.com.zsc.util.ConstValue;

import com.jarp.db.DAO;

public class GroupManage {

	DAO dao = new DAO();

	public GroupManage() {
	};

	/**
	 * 异步获取分类信息
	 * 
	 * @param plcode
	 *            plcode 编号
	 * @param clcode
	 *            节点编号
	 * @return 分类的JSON字符串
	 */
	@SuppressWarnings("unchecked")
	public String getGroup(String plcode, String clcode) {

		// 查询次根目录下的节点
		String specialRootSQL = "select cl_code,pl_code,idx,remarked,cl_name,cl_pcode,parm from cm_commList where pl_code = '"
				+ plcode + "' and cl_pcode='" + clcode + "' order by parm,idx";

		List dataList = new ArrayList();
		try {
			Vector v = dao.getDataSet(ConstValue.DBNAME, specialRootSQL);

			if (v != null) {
				for (int i = 0; i < v.size(); i++) {
					Hashtable ht = (Hashtable) v.get(i);

					// 需要遍历的SQL,根据查询到得
					String traversalsql = "select count(cl_pcode) as countnum from cm_commlist where cl_pcode ='"+ht.get("CL_CODE").toString().trim()+"'";
					int n = Integer.parseInt(dao.getDataValue(ConstValue.DBNAME, traversalsql));
					GroupVO vo = new GroupVO();
					if(n==0) vo.setLeaf(true);
					else vo.setLeaf(false);
					
					vo.setPlCode(ht.get("PL_CODE").toString().trim());
					vo.setClCode(ht.get("CL_CODE").toString().trim());
					vo.setClName(ht.get("CL_NAME").toString().trim());
					vo.setClPcode(ht.get("CL_PCODE").toString().trim());
					vo.setText(vo.getClName());
					vo.setParm(ht.get("PARM").toString().trim());
					vo.setId(ht.get("CL_CODE").toString().trim());
					vo.setSort(ht.get("IDX").toString().trim());
					vo.setRemarked(ht.get("REMARKED").toString().trim());
					vo.setParm(ht.get("PARM").toString().trim());

					dataList.add(vo);
					
				}
				return JSONArray.fromObject(dataList).toString();
			}
			return ConstValue.EMPTY_RECORD_NOT_PAGEING;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>"
					+ e.getClass().getName()
					+ ".getGroup(String id,String pcode)中" + e.getMessage());
			return ConstValue.EMPTY_RECORD_NOT_PAGEING;
		}
	}

	/**
	 * 增加指标分类
	 * 
	 * @param dataOb
	 * @return 是否成功
	 */
	public String addGroup(String dataOb) {
		JSONObject json = JSONObject.fromString(dataOb);

		String remarked = json.getString("remarked").trim();
		String clname = json.getString("text").trim();
		String plcode = json.getString("cid").trim();
		String clcode = json.getString("pid").trim();
		String parm = json.getString("parm").trim();
		
		try {
			String checkNameSQL = "select count(cl_name) from cm_commlist where parm='"
					+ plcode
					+ "' and cl_pcode ='"
					+ clcode
					+ "' and cl_name ='" + clname + "'";

			String ct = dao.getDataValue(ConstValue.DBNAME, checkNameSQL);
			if (!"0".equals(ct))
				return "该分类已经存在,请重新命名分类";
			String getIdxSQL = "select count(idx) as sort from cm_commlist where parm='"
					+ plcode + "' and cl_pcode='" + clcode + "'";
			String pos_s = dao.getDataValue(ConstValue.DBNAME, getIdxSQL);
			int pos = 0;
			if (!"".equals(pos_s))
				pos = Integer.parseInt(pos_s) + 1;

			
			String insertSQL = "insert into cm_commlist (CL_CODE,PL_CODE,CL_NAME,CL_PCODE,PARM,IDX,REMARKED)"
					+ "values (default,'"
					+ plcode
					+ "','"
					+ clname
					+ "','"
					+ clcode
					+ "','"+parm+"','" + pos + "','"+remarked+"')";

			dao.execCommand(ConstValue.DBNAME, insertSQL);
			return "success";
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>"
					+ this.getClass().getName() + ".AddGroup(String dataOb)"
					+ ex.getMessage());
			return ex.getMessage();
		}
	}

	/**
	 * 删除指标分类
	 * 
	 * @param dataOb
	 * @return 是否成功
	 */
	public String delGroup(String dataOb) {
		JSONObject json = JSONObject.fromString(dataOb);
		String ccode = json.getString("ccode").trim();
		
		ArrayList sqlList = new ArrayList();
		String SQL = "DELETE FROM cm_commlist WHERE cl_code= '" + ccode+ "'";
		sqlList.add(SQL);
		SQL = "DELETE FROM cm_commlist WHERE cl_pcode='" + ccode+ "'";
		sqlList.add(SQL);
		try {
			dao.execCommand(ConstValue.DBNAME, sqlList);
			return "success";
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>"
					+ this.getClass().getName() + ".delGroup(String dataOb)"
					+ ex.getMessage());
			return ex.getMessage();
		}
	}

	/**
	 * 修改分类名称
	 * 
	 * @param dataOb
	 * @return
	 */
	public String updateGroup(String dataOb) {
		JSONObject json = JSONObject.fromString(dataOb);
		
		String remarked = json.getString("remarked").trim();
		String text = json.getString("text").trim();
		String ccode = json.getString("ccode").trim();
		String parm = json.getString("parm").trim();
		
		String SQL = "UPDATE cm_commlist SET cl_name='" + text
				+ "',remarked = '"+remarked+"',parm = '"+parm+"' WHERE cl_code='" + ccode + "'";
		try {
			dao.execCommand(ConstValue.DBNAME, SQL);
			return "success";
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>"
					+ this.getClass().getName() + ".updateGroup(String dataOb)"+dataOb
					+ ex.getMessage());
			return ex.getMessage();
		}
	}

	/**
	 * 保存当前树的结构信息
	 * 
	 * @param dataOb
	 *            [nodeID,pNodeID_nodePos]
	 * @return 是否成功
	 */
	public String saveNodes(String cid, String dataOb) {
		/* 将前台的JSONArray转化为List */
		List nodesList = new ArrayList();
		JSONArray ddArr = JSONArray.fromString(dataOb);
		for (int i = 0; i < ddArr.length(); i++) {
			JSONArray arr = ddArr.getJSONArray(i);
			List nodes = (ArrayList) JSONArray.toList(arr,
					java.util.ArrayList.class);
			nodesList.add(nodes);
		}
		/* 更新节点的分类 */
		ArrayList sqlList = new ArrayList();
		for (int i = 0; i < nodesList.size(); i++) {
			ArrayList node = (ArrayList) nodesList.get(i);
			String id = node.get(0).toString().trim();
			String pid = node.get(1).toString().trim().split("_")[0];
			//当前节点的cl_code
			String clcode = node.get(0).toString().trim();
			//父节点的cl_code
			String plcode = node.get(1).toString().trim().split("_")[0];
			String SQL = "UPDATE cm_commlist SET cl_pcode='" + plcode
					+ "' WHERE cl_code='" + clcode + "' AND pl_code=" + cid;
			sqlList.add(SQL);
		}
		/* 更新节点的顺序 */
		for (int i = 0; i < nodesList.size(); i++) {
			ArrayList node = (ArrayList) nodesList.get(i);
			String id = node.get(0).toString().trim();
			String pid = node.get(1).toString().trim().split("_")[0];
			String nodePos = node.get(1).toString().trim().split("_")[1];
			
			//当前节点的cl_code
			String clcode = node.get(0).toString().trim();
			//父节点的cl_code
			String plcode = node.get(1).toString().trim().split("_")[0];
			//节点顺序idx
			String nodePosa = node.get(1).toString().trim().split("_")[1];
			
			String test = "update cm_commlist set idx='"+nodePosa+"' where  cl_code ='"+clcode+"'";
			
			sqlList.add(test);
		}
		try {
			dao.execCommand(ConstValue.DBNAME, sqlList);
			return "success";
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>"
					+ this.getClass().getName()
					+ ".saveNodes(String cid,String dataOb)" + ex.getMessage());
			return ex.getMessage();
		}
	}
}
